package cn.fl.insroute.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.fl.insroute.condition.InsRoutePrdInfoQC;
import cn.fl.insroute.service.IInsRouteBscInfoService;
import cn.fl.insroute.service.IInsRoutePrdInfoService;
import cn.fl.insroute.vo.InsRouteBscInfoVO;
import cn.fl.insroute.vo.InsRoutePrdInfoVO;
import cn.fl.preinsroute.vo.PreRoutePrdInfoVO;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.preinsroute.service.IPreRouteBscInfoService;
import cn.fl.preinsroute.vo.PreRouteBscInfoVO;
import cn.fl.preinsroute.vo.PreRouteInsResVO;
import cn.fl.preinsroute.vo.PreRoutePrpsFeeVO;
import cn.fl.project.constant.PrjQuatPrpsConstant;
import cn.fl.project.constant.PrjQuatPrpsFeeConstant;
import cn.fl.project.service.ICalQuatPrpsService;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.vo.CalQuatPrpsFeeVO;
import cn.fl.project.vo.CalQuatPrpsVO;
import cn.fl.project.vo.PrjBscInfoVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.fl.insroute.condition.InsRoutePrpsFeeQC;
import cn.fl.insroute.service.IInsRoutePrpsFeeService;
import cn.fl.insroute.vo.InsRoutePrpsFeeVO;
import cn.fl.framework.base.context.CurrentThreadContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.fl.system.constant.SysLogConstant;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;

/**
 * @Description: 路由方案-费用项 控制层
 */
@Controller
@RequestMapping("/insroute/insRoutePrpsFee")
public class InsRoutePrpsFeeController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(InsRoutePrpsFeeController.class);

	/**
	 * InsRoutePrpsFee 服务层BEAN
	 */
	@Autowired
	private IInsRoutePrpsFeeService insRoutePrpsFeeService;

	@Autowired
	private IInsRouteBscInfoService insRouteBscInfoService;

	@Autowired
	private IPrjBscInfoService prjBscInfoService;

	@Autowired
	private ICalQuatPrpsService calQuatPrpsService;

	@Autowired
	private IPreRouteBscInfoService preRouteBscInfoService;

	@Autowired
	private IInsRoutePrdInfoService insRoutePrdInfoService;


	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("insroute/insRoutePrpsFee/list");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("insroute/insRoutePrpsFee/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		InsRoutePrpsFeeQC qc = new InsRoutePrpsFeeQC();
		if (StringUtils.isNotEmpty(searchName)) {
			//TODO SOMTHING
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = new Pager();//this.insRoutePrpsFeeService.searchListPage(page);
		List<InsRoutePrpsFeeVO> list = (List<InsRoutePrpsFeeVO>) pager.getResultList();
		ListPageVO<InsRoutePrpsFeeVO> listPage = new ListPageVO<InsRoutePrpsFeeVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 初始化路由费用项信息
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initRoutePrpsFee.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initRoutePrpsFee(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"initRoutePrpsFee","初始化路由费用项信息.请求时间：" + startTm + "-请求参数：" + this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName());
		String prjId = request.getParameter("prjId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "初始化路由费用项信息成功");
		ListPageVO<InsRoutePrpsFeeVO> listPage = null;
		try {
			if(StringUtils.isEmpty(prjId)){
				BaseLogUtils.info(logger,"initRoutePrpsFee","项目ID为空");
				throw new BaseException("项目ID为空");
			}

			PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
			String prjTypCd = prjBscInfoVO.getPrjTypCd();
			BaseLogUtils.info(logger,"initRoutePrpsFee","初始化路由费用项信息.项目信息prjBscInfoVO：" + JSON.toJSONString(prjBscInfoVO),CurrentThreadContext.getCurrentUserName());
			if(prjBscInfoVO == null){
				throw new BaseException("项目信息异常");
			}
			/**
			 * 2.查询该项目有否添加过资管路由数据（没有初始化数据，有返回显示）
			 */
			InsRouteBscInfoVO insRouteBscInfoVO = this.insRouteBscInfoService.searchListByPrjId(Long.valueOf(prjId));
			BaseLogUtils.info(logger,"initRoutePrpsFee","初始化路由费用项信息.查询路由数据结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
			if(insRouteBscInfoVO == null){
				/**
				 * 厂商业务特殊处理
				 */
				insRouteBscInfoVO = new InsRouteBscInfoVO();
				if(prjBscInfoVO != null && BaseConstant.IS_YESNO_YES == prjBscInfoVO.getIsSplMfr() && PrjBscInfoConstant.CODE_BU_ORG_CD_ZK.equals(prjBscInfoVO.getBuOrgCd())){
					insRouteBscInfoVO.setInsRoutePrpsFeeVOList(this.initInsRoutePrpsFeeVOList(prjTypCd));
					insRouteBscInfoVO.setPrjId(Long.valueOf(prjId));
					insRouteBscInfoVO = this.setInsRouteBscInfo(prjBscInfoVO.getCalId(),insRouteBscInfoVO);
					BaseLogUtils.info(logger,"initRoutePrpsFee","初始化资管路由数据.查询路由数据.厂商业务特殊处理结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
				}else{
					Long preRouteId = prjBscInfoVO.getPreRouteId();
					Long preResId = prjBscInfoVO.getPreResId();
					if(preRouteId != null && preResId!= null){
						PreRouteBscInfoVO preRouteBscInfoVO = this.preRouteBscInfoService.selectPreRouteBscInfoAllById(preRouteId);
						List<PreRoutePrdInfoVO> preRoutePrdInfoVOS = preRouteBscInfoVO.getPreRoutePrdInfoVOS();
						if(CollectionUtils.isEmpty(preRoutePrdInfoVOS)){
							throw new BaseException("路由预筛选信息异常");
						}
						List<PreRoutePrpsFeeVO> preRoutePrpsFeeVOStemp = new ArrayList<PreRoutePrpsFeeVO>();
						List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = new ArrayList<InsRoutePrpsFeeVO>();
						Double totAmt = 0d;//合同额预筛选
						Double contAmt = 0d;//合同额路由
						List<PreRoutePrpsFeeVO> preRoutePrpsFees = new ArrayList<PreRoutePrpsFeeVO>();
						if(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
								|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
								|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd)){
							InsRoutePrdInfoQC insRoutePrdInfoQC = new InsRoutePrdInfoQC();
							insRoutePrdInfoQC.setPrjId(Long.valueOf(prjId));
							List<InsRoutePrdInfoVO> insRoutePrdInfoVOS = this.insRoutePrdInfoService.searchList(insRoutePrdInfoQC);
							BaseLogUtils.info(logger,"initRoutePrpsFee","初始化路由费用项信息.查询路由产品insRoutePrdInfoVOS：" + JSON.toJSONString(insRoutePrdInfoVOS),CurrentThreadContext.getCurrentUserName());
							if(CollectionUtils.isNotEmpty(insRoutePrdInfoVOS)){
								for (InsRoutePrdInfoVO insRoutePrdInfoVO : insRoutePrdInfoVOS) {
									Long mediationFeeAmt = insRoutePrdInfoVO.getMediationFeeAmt();
									if(mediationFeeAmt == null){
										mediationFeeAmt = 0l;
									}
									contAmt += (insRoutePrdInfoVO.getGuidePc()+mediationFeeAmt)*insRoutePrdInfoVO.getPrdQty();
								}
							}
						}
						for (PreRoutePrdInfoVO preRoutePrdInfoVO : preRoutePrdInfoVOS) {
							if(preResId.equals(preRoutePrdInfoVO.getResId())){
								if(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
										|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
										|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd)){
									Long mediationFeeAmt = preRoutePrdInfoVO.getMediationFeeAmt();
									if(mediationFeeAmt == null){
										mediationFeeAmt = 0l;
									}
									totAmt += (preRoutePrdInfoVO.getGuidePc() + mediationFeeAmt)*preRoutePrdInfoVO.getPrdQty();
								}else{
									totAmt += (preRoutePrdInfoVO.getGuidePc()*preRoutePrdInfoVO.getPrdQty());
								}
								preRoutePrpsFees = preRoutePrdInfoVO.getPreRoutePrpsFeeVOS();
								preRoutePrpsFeeVOStemp.addAll(preRoutePrdInfoVO.getPreRoutePrpsFeeVOS());
							}
						}
						totAmt = Double.valueOf(DecimalUtils.convertYuanToFen(totAmt));
						Double actAmt = totAmt;
						Double fee01 = 0d;
						Double fee02 = 0d;
						String feeCalBaseCd = PrjQuatPrpsConstant.CODE_CAL_BASE_FINANCING;
						for(PreRoutePrpsFeeVO preRoutePrpsFeeVO : preRoutePrpsFeeVOStemp){
							if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01.equals(preRoutePrpsFeeVO.getFeeTypCd())){
								fee01 += DecimalUtils.convertYuanToFen(preRoutePrpsFeeVO.getFeeAmt());
								actAmt -= DecimalUtils.convertYuanToFen(preRoutePrpsFeeVO.getFeeAmt());
							}
							if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_02.equals(preRoutePrpsFeeVO.getFeeTypCd())){
								feeCalBaseCd = preRoutePrpsFeeVO.getCalBaseCd();
								fee02 += DecimalUtils.convertYuanToFen(preRoutePrpsFeeVO.getFeeAmt());
							}
						}
						if(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
								|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
								|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd)){
							Double pct01 = DecimalUtils.div(fee01,totAmt,10);
							Double pct02 = DecimalUtils.div(fee02,actAmt,10);
							if(PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT.equals(feeCalBaseCd)){
								pct02 = DecimalUtils.div(fee02,totAmt,10);
							}
							for(PreRoutePrpsFeeVO preRoutePrpsFeeVO : preRoutePrpsFees){
								InsRoutePrpsFeeVO insRoutePrpsFeeVO = new InsRoutePrpsFeeVO();
								insRoutePrpsFeeVO.setFeeTypCd(preRoutePrpsFeeVO.getFeeTypCd());
								insRoutePrpsFeeVO.setCntWayCd(preRoutePrpsFeeVO.getCntWayCd());
								insRoutePrpsFeeVO.setCalBaseCd(preRoutePrpsFeeVO.getCalBaseCd());
								insRoutePrpsFeeVO.setIsAllowChg(BaseConstant.IS_YESNO_YES);
								insRoutePrpsFeeVO.setIsFixedAmt(preRoutePrpsFeeVO.getIsFixedAmt());
								insRoutePrpsFeeVO.setIsSysDef(preRoutePrpsFeeVO.getIsSysDef());
								if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01.equals(preRoutePrpsFeeVO.getFeeTypCd())){
									insRoutePrpsFeeVO.setFeeAmt((pct01*contAmt));
									insRoutePrpsFeeVO.setFeePct(pct01*100);
								}
								if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_02.equals(preRoutePrpsFeeVO.getFeeTypCd())){
									insRoutePrpsFeeVO.setFeePct(pct02*100);
									if(PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT.equals(preRoutePrpsFeeVO.getCalBaseCd())){
										insRoutePrpsFeeVO.setFeeAmt((pct02*contAmt));
									}else{
										insRoutePrpsFeeVO.setFeeAmt((pct02*(contAmt*(1-pct01))));
									}
								}
								insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO);
							}
						}else{
							for(PreRoutePrpsFeeVO preRoutePrpsFeeVO : preRoutePrpsFees){
								InsRoutePrpsFeeVO insRoutePrpsFeeVO = new InsRoutePrpsFeeVO();
								insRoutePrpsFeeVO.setFeeTypCd(preRoutePrpsFeeVO.getFeeTypCd());
								insRoutePrpsFeeVO.setCntWayCd(preRoutePrpsFeeVO.getCntWayCd());
								insRoutePrpsFeeVO.setCalBaseCd(preRoutePrpsFeeVO.getCalBaseCd());
								insRoutePrpsFeeVO.setIsAllowChg(preRoutePrpsFeeVO.getIsAllowChg());
								insRoutePrpsFeeVO.setIsFixedAmt(preRoutePrpsFeeVO.getIsFixedAmt());
								insRoutePrpsFeeVO.setIsSysDef(preRoutePrpsFeeVO.getIsSysDef());
								if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01.equals(preRoutePrpsFeeVO.getFeeTypCd())){
									insRoutePrpsFeeVO.setFeeAmt(fee01);
									insRoutePrpsFeeVO.setFeePct(DecimalUtils.div(fee01,totAmt)*100);
								}
								if(PrjQuatPrpsFeeConstant.FEE_TYP_CD_02.equals(preRoutePrpsFeeVO.getFeeTypCd())){
									insRoutePrpsFeeVO.setFeeAmt(fee02);
									if(PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT.equals(preRoutePrpsFeeVO.getCalBaseCd())){
										insRoutePrpsFeeVO.setFeePct(DecimalUtils.div(fee02,totAmt)*100);
									}else{
										insRoutePrpsFeeVO.setFeePct(DecimalUtils.div(fee02,actAmt)*100);
									}
								}
								insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO);
							}
						}
						insRouteBscInfoVO.setInsRoutePrpsFeeVOList(insRoutePrpsFeeVOList);
					}else{
						insRouteBscInfoVO.setInsRoutePrpsFeeVOList(this.initInsRoutePrpsFeeVOList(prjTypCd));
					}
				}
			}
			List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = insRouteBscInfoVO.getInsRoutePrpsFeeVOList();
			List<InsRoutePrpsFeeVO> prpsFeeList = new ArrayList<InsRoutePrpsFeeVO>();
			if(CollectionUtils.isNotEmpty(insRoutePrpsFeeVOList)){
				for (InsRoutePrpsFeeVO insRoutePrpsFeeVO : insRoutePrpsFeeVOList) {
					String feeTypCd = insRoutePrpsFeeVO.getFeeTypCd();
					String calBaseCd = insRoutePrpsFeeVO.getCalBaseCd();
					String cntWayCd = insRoutePrpsFeeVO.getCntWayCd();
					if(StringUtils.isNotEmpty(feeTypCd)){
						SysDictionaryDTO sysDictionaryDTO = this.insRouteBscInfoService.getFeeTypCdNm(feeTypCd);
						if(sysDictionaryDTO != null){
							insRoutePrpsFeeVO.setFeeTypCdNm(sysDictionaryDTO.getCodeNm());
						}
						if(StringUtils.isNotEmpty(calBaseCd)){
							SysDictionaryDTO sysDictionaryDTOcalBaseCd = this.insRouteBscInfoService.getCalBaseCdNm(calBaseCd);
							if(sysDictionaryDTO != null){
								insRoutePrpsFeeVO.setCalBaseCdNm(sysDictionaryDTOcalBaseCd.getCodeNm());
							}
						}
						if(StringUtils.isNotEmpty(cntWayCd)){
							SysDictionaryDTO sysDictionaryDTOcalBaseCdcntWayCd = this.insRouteBscInfoService.getCntWayCdNm(cntWayCd);
							if(sysDictionaryDTO != null){
								insRoutePrpsFeeVO.setCntWayCdNm(sysDictionaryDTOcalBaseCdcntWayCd.getCodeNm());
							}
						}
						if (PrjQuatPrpsFeeConstant.FEE_TYP_CD_01.equals(feeTypCd)
								|| PrjQuatPrpsFeeConstant.FEE_TYP_CD_02.equals(feeTypCd)) {
							if(insRoutePrpsFeeVO.getFeeAmt() != null){
								Double round = DecimalUtils.round(insRoutePrpsFeeVO.getFeeAmt(), 0);
								insRoutePrpsFeeVO.setFeeAmt(DecimalUtils.convertFenToYuan(round.longValue()));
							}
							prpsFeeList.add(insRoutePrpsFeeVO);
						}
					}
				}
			}
			Collections.sort(prpsFeeList,new sortFee());
			listPage = new ListPageVO<InsRoutePrpsFeeVO>(prpsFeeList, prpsFeeList.size());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "initRoutePrpsFee", "初始化路由费用项信息.异常" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.info(logger, "initRoutePrpsFee", "初始化路由费用项信息.异常" + ex.getStackTrace(), CurrentThreadContext.getCurrentUserName());
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger,"initRoutePrpsFee","初始化路由费用项信息.返回结果：" + JSON.toJSONString(listPage) + "-耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	class sortFee implements Comparator<InsRoutePrpsFeeVO> {
		@Override
		public int compare(InsRoutePrpsFeeVO o1, InsRoutePrpsFeeVO o2) {
			return o1.getFeeTypCd().compareTo(o2.getFeeTypCd());
		}
	}

	/**
	 * 新增初始化费用项数据
	 * @return
	 * @throws BaseException
	 */
	private List<InsRoutePrpsFeeVO> initInsRoutePrpsFeeVOList(String prjTypCd) throws BaseException {
		List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = new ArrayList<InsRoutePrpsFeeVO>();
		InsRoutePrpsFeeVO insRoutePrpsFeeVO01 = new InsRoutePrpsFeeVO();
		insRoutePrpsFeeVO01.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01);
		insRoutePrpsFeeVO01.setFeePct(0d);
		insRoutePrpsFeeVO01.setFeeAmt(0d);
		insRoutePrpsFeeVO01.setCalBaseCd(PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT);
		insRoutePrpsFeeVO01.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_DOWNPAY);
		insRoutePrpsFeeVO01.setIsAllowChg(BaseConstant.IS_YESNO_YES);
		insRoutePrpsFeeVO01.setIsFixedAmt(BaseConstant.IS_YESNO_NO);
		insRoutePrpsFeeVO01.setFeeTypCdNm("首期");
		insRoutePrpsFeeVO01.setCalBaseCdNm("合同额");
		insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO01);

		InsRoutePrpsFeeVO insRoutePrpsFeeVO02 = new InsRoutePrpsFeeVO();
		insRoutePrpsFeeVO02.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_02);
		insRoutePrpsFeeVO02.setFeePct(0d);
		insRoutePrpsFeeVO02.setFeeAmt(0d);
		insRoutePrpsFeeVO02.setCalBaseCd(PrjQuatPrpsConstant.CODE_CAL_BASE_FINANCING);
		insRoutePrpsFeeVO02.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_DOWNPAY);
		insRoutePrpsFeeVO02.setIsAllowChg(BaseConstant.IS_YESNO_YES);
		insRoutePrpsFeeVO02.setIsFixedAmt(BaseConstant.IS_YESNO_NO);
		insRoutePrpsFeeVO02.setFeeTypCdNm("客户保证金");
		insRoutePrpsFeeVO02.setCalBaseCdNm("融资额");
		insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO02);
		if(!(PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd)
				|| PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd)
				|| PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd))){
			InsRoutePrpsFeeVO insRoutePrpsFeeVO63 = new InsRoutePrpsFeeVO();
			insRoutePrpsFeeVO63.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_63);
			insRoutePrpsFeeVO63.setFeePct(0d);
			insRoutePrpsFeeVO63.setFeeAmt(0d);
			insRoutePrpsFeeVO63.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
			insRoutePrpsFeeVO63.setIsAllowChg(BaseConstant.IS_YESNO_YES);
			insRoutePrpsFeeVO63.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
			insRoutePrpsFeeVO63.setFeeTypCdNm("发票金额");
			insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO63);

			InsRoutePrpsFeeVO insRoutePrpsFeeVO64 = new InsRoutePrpsFeeVO();
			insRoutePrpsFeeVO64.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_64);
			insRoutePrpsFeeVO64.setFeePct(0d);
			insRoutePrpsFeeVO64.setFeeAmt(0d);
			insRoutePrpsFeeVO64.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
			insRoutePrpsFeeVO64.setIsAllowChg(BaseConstant.IS_YESNO_YES);
			insRoutePrpsFeeVO64.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
			insRoutePrpsFeeVO64.setFeeTypCdNm("补票");
			insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO64);
		}
		return insRoutePrpsFeeVOList;
	}

	/**
	 * 设置路由信息（厂商业务）
	 * @param calId
	 * @return
	 * @throws BaseException
	 */
	private InsRouteBscInfoVO setInsRouteBscInfo(Long calId,InsRouteBscInfoVO insRouteBscInfoVO) throws BaseException{
		BaseLogUtils.info(logger,"setInsRouteBscInfo","设置路由信息（厂商业务）.请求参数calId：" + calId + "-insRouteBscInfoVO:" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
		if(calId == null){
			throw new BaseException("厂商报价器ID为空");
		}
		CalQuatPrpsVO calQuatPrpsVO = this.calQuatPrpsService.selectCalQuatProsAndDetailById(calId);
		BaseLogUtils.info(logger,"setInsRouteBscInfo","设置路由信息（厂商业务）.查询厂商方案结果calQuatPrpsVO：" + JSON.toJSONString(calQuatPrpsVO),CurrentThreadContext.getCurrentUserName());
		if(calQuatPrpsVO != null){
			insRouteBscInfoVO.setAnnIntRatePct(calQuatPrpsVO.getAnnIntRatePct());
			insRouteBscInfoVO.setLsPd(calQuatPrpsVO.getLsPd());
			if(calQuatPrpsVO.getContAmt() == null){
				throw new BaseException("厂商报价器合同额为空");
			}
			insRouteBscInfoVO.setContAmt(DecimalUtils.convertYuanToFen(calQuatPrpsVO.getContAmt()).doubleValue());
			List<CalQuatPrpsFeeVO> calQuatPrpsFeeVOList = calQuatPrpsVO.getFeeList();
			List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = insRouteBscInfoVO.getInsRoutePrpsFeeVOList();
			if(CollectionUtils.isNotEmpty(calQuatPrpsFeeVOList) && CollectionUtils.isNotEmpty(insRoutePrpsFeeVOList)){
				for (InsRoutePrpsFeeVO insRoutePrpsFeeVO : insRoutePrpsFeeVOList) {
					for(CalQuatPrpsFeeVO calQuatPrpsFeeVO : calQuatPrpsFeeVOList){
						if(StringUtils.isNotEmpty(calQuatPrpsFeeVO.getFeeTypCd())
								&& (calQuatPrpsFeeVO.getFeeTypCd()).equals(insRoutePrpsFeeVO.getFeeTypCd())){
							insRoutePrpsFeeVO.setPrjId(insRouteBscInfoVO.getPrjId());
							insRoutePrpsFeeVO.setIsFixedAmt(calQuatPrpsFeeVO.getIsFixedAmt());
							insRoutePrpsFeeVO.setIsAllowChg(BaseConstant.IS_YESNO_NO);
                            insRoutePrpsFeeVO.setCntWayCd(calQuatPrpsFeeVO.getCntWayCd());
                            insRoutePrpsFeeVO.setCntWayCdNm(calQuatPrpsFeeVO.getCntWayCdNm());
                            insRoutePrpsFeeVO.setCalBaseCd(calQuatPrpsFeeVO.getCalBaseCd());
                            insRoutePrpsFeeVO.setCalBaseCdNm(calQuatPrpsFeeVO.getCalBaseCdNm());
							if(calQuatPrpsFeeVO.getFeeAmt() != null){
								insRoutePrpsFeeVO.setFeeAmt(DecimalUtils.convertYuanToFen(calQuatPrpsFeeVO.getFeeAmt()).doubleValue());
							}else{
								insRoutePrpsFeeVO.setFeeAmt(0d);
							}
							insRoutePrpsFeeVO.setFeePct(calQuatPrpsFeeVO.getFeePct());
							insRoutePrpsFeeVO.setFeeTypCd(calQuatPrpsFeeVO.getFeeTypCd());
						}
					}
				}
			}
			insRouteBscInfoVO.setInsRoutePrpsFeeVOList(insRoutePrpsFeeVOList);
		}
		BaseLogUtils.info(logger,"setInsRouteBscInfo","设置路由信息（厂商业务）.返回结果insRouteBscInfoVO：" + JSON.toJSONString(insRouteBscInfoVO),CurrentThreadContext.getCurrentUserName());
		return insRouteBscInfoVO;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.insRoutePrpsFeeService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			InsRoutePrpsFeeVO vo = this.insRoutePrpsFeeService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		InsRoutePrpsFeeVO entity = null;
		try {
			entity = new InsRoutePrpsFeeVO();
			//TODO ADD PARAM
			Long id = this.insRoutePrpsFeeService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		InsRoutePrpsFeeVO entity = null;
		try {
			entity = new InsRoutePrpsFeeVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.insRoutePrpsFeeService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
}

